<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="jxl.*" %>
<%@ page import="jxl.write.*" %>
<% 
	request.setCharacterEncoding("UTF-8");
	//将stuId、stuName、birth等参数传递过来
	String stuId = request.getParameter("stuId").trim();
	String stuName = request.getParameter("stuName").trim();;
	String birth = request.getParameter("birth").trim();
	String stuTele = request.getParameter("stuTele").trim();
	String stuQQ = request.getParameter("stuQQ").trim();
	Calendar cal = Calendar.getInstance();   			/*用Calendar函数获取一个实例*/
	cal.set(Integer.parseInt(birth.split("/")[0]),Integer.parseInt(birth.split("/")[1])-1,Integer.parseInt(birth.split("/")[2]));
	Date date = cal.getTime();  				/*取得当前时间*/
	SimpleDateFormat datef = new SimpleDateFormat("yyyy年MM月dd日");		 /*新建一个对象，并 设定格式*/
	birth = datef.format(date).toString(); 			 /*将date转化为字符串类型*/
	File fileName = new File("D:/workplace/stuinfo.xls");
	try{
		if(!fileName.exists()){
			WritableWorkbook workbook  =  Workbook.createWorkbook (fileName); /*新建一个workbook对象*/
			WritableSheet sheettemp = workbook.createSheet("sheet1", 0);  /*新建一个工作表，命名“sheet1”*/
			//在第一行的单元格，一次写入学号。姓名等信息
			Label label = new Label(0,0,"学号");
			sheettemp.addCell(label);
			label = new Label(1,0,"姓名");
			sheettemp.addCell(label);
			label = new Label(2,0,"出生日期");
			sheettemp.addCell(label);
			label = new Label(3,0,"联系电话");
			sheettemp.addCell(label);
			label = new Label(4,0,"QQ号码");
			sheettemp.addCell(label);
			label = new Label(10,0,"password");
			sheettemp.addCell(label);
			workbook.write();  	/*将信息写入到工作表中*/
			workbook.close();	/*关闭工作空间*/
		}
		Workbook book = Workbook.getWorkbook(fileName); /*创建一个book工作表对象 */
		Sheet sheet = book.getSheet(0);  		/*得到s工作表的第一张表*/
		int len = sheet.getRows();			/*取得行数*/
		String tempstr = null;
		
		for(int i=0;i<len;i++)
		{
			tempstr = sheet.getCell(0, i).getContents();
			
			//用循环语句，取得各行第一列的数据，将之与新输入的学号进行对比，若相等，则提示信息已经存在
			if(stuId.equals(tempstr))
			{
				out.print("<script>alert('学生信息已存在，请返回重新输入！');</script>");
				out.print("<script>window.location='managerinputinfo.jsp';</script>");
				return ;
			}
		}
		WritableWorkbook workbook  =  Workbook.createWorkbook (fileName,book); /*创建一个可写的工作表，指向book*/
		WritableSheet sheet1 = workbook.getSheet(0);  /*得到第一张工作表*/
		//依次将输入的信息写入工作表的最后一行
		Label label1 = new Label(0,len,stuId);
		sheet1.addCell(label1);
		label1 = new Label(1,len,stuName);
		sheet1.addCell(label1);
		label1 = new Label(2,len,birth);
		sheet1.addCell(label1);
		label1 = new Label(3,len,stuTele);
		sheet1.addCell(label1);
		label1 = new Label(4,len,stuQQ);
		sheet1.addCell(label1);
		label1 = new Label(10,len,stuId);
		sheet1.addCell(label1);
		workbook.write();		 /*关闭工作表*/
		out.print("<script>alert('学生信息已经成功输入，请返回！');</script>");
		out.print("<script>window.location='managerinputinfo.jsp';</script>");
		workbook.close();			
		book.close(); 
	}catch(Exception e){
		out.print(e.getMessage());
	} 
		
%>